package com.newmodelclient.baselib.presenter;

import com.newmodelclient.baselib.container.CommonBaseResponse;
import com.newmodelclient.baselib.enums.FCPageViewState;
import com.newmodelclient.baselib.notify.RxManager;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 文件名：FCBasePresenter
 * 描 述：底层Presenter
 * 作 者：gyq
 * 时 间：2021-1-22
 */
public class FCBasePresenter<V extends IFCBaseView> {

    public final static String SERVER_FAILED_STR = "服务异常";
    public final static String WEB_FAILED_STR = "网络异常，请重试";

    public static final int NET_SUCCESS_CODE = 0;

    //暂时兼容
    public RxManager mRxManager = new RxManager();

    //接口
    protected V mView;
    public void setIView(V v) {
        this.mView = v;
    }
    //

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                     网络请求
    //
    /////////////////////////////////////////////////////////////////////////////////

    /**
     * 添加线程管理并订阅
     * 兼容方法
    * */
    public Disposable toSubscribe(Observable observable, Consumer next, Consumer error){
        return  observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(next,error);
    }


    /**
     * 请求订阅
     * @param observable 订阅函数
     * @param callBack 回调
     * */
    public <O1> Disposable toNewSubscribe(Observable observable, FCBaseCallBack<CommonBaseResponse<O1>> callBack) {
        Disposable disposable = toNewSubscribe(observable,"",callBack);
        return disposable;
    }

    /**
     * 请求订阅
     * @param observable 订阅函数
     * @param identifier 请求标示
     * @param callBack 回调
    * */
    public <O1> Disposable toNewSubscribe(Observable observable, String identifier, FCBaseCallBack<CommonBaseResponse<O1>> callBack) {
        Disposable disposable =  observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonBaseResponse<O1>>() {
                    @Override
                    public void accept(CommonBaseResponse<O1> bean) throws Exception {
                        // 有的接口成功返回的状态是0
                        if (callBack != null) {
                            if (bean != null) {
                                if (bean.getCode() == NET_SUCCESS_CODE) {
                                    successHiddenLoading(identifier);
                                    callBack.onSuccess(bean);
                                } else {
                                    failedHiddenLoading(identifier);
                                    callBack.onServiceError(bean);
                                }
                            } else {
                                failedHiddenLoading(identifier);
                                CommonBaseResponse<O1>temp = new CommonBaseResponse<>();
                                temp.code = -8888;
                                temp.msg = SERVER_FAILED_STR;
                                callBack.onServiceError(temp);
                            }
                        }
                    }
                },new ThrowableConsumer() {
                    @Override
                    public void accept(Throwable o) throws Exception {
                        super.accept(o);
                        newErrorHiddenLoading(identifier);
                        if (callBack != null) {
//                            callBack.onNetWorkError(WEB_FAILED_STR);
                        }
                    }
                });
        return disposable;
    }


    /////////////////////////////////////////////////////////////////////////////////
    //
    //                    隐藏loading
    //
    /////////////////////////////////////////////////////////////////////////////////

    /**
     * 成功时隐藏
     * */
    protected void successHiddenLoading(String identifier) {
        if (mView != null) {
            mView.baseHiddenCommitLoading();
            mView.baseHiddenPageLoading();
            mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL,"");
        }
    }

    /**
     * 服务失败时隐藏
     * */
    protected void failedHiddenLoading(String identifier) {
        if (mView != null) {
            mView.baseHiddenCommitLoading();
            mView.baseHiddenPageLoading();
        }
    }

    /**
     * 网络失败时隐藏
     * */
    protected void newErrorHiddenLoading(String identifier) {
        if (mView != null) {
            mView.baseHiddenCommitLoading();
            mView.baseHiddenPageLoading();
        }
    }




    /////////////////////////////////////////////////////////////////////////////////
    //
    //                    释放/cancel请求
    //
    /////////////////////////////////////////////////////////////////////////////////

    public void onDestroy() {
        if (mRxManager != null) {
            mRxManager.clear();
            mRxManager = null;
        }
    }


}
